package com.wxj.bank.storage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wxj.bank.module.constant.Constants;
import com.wxj.bank.module.page.PageBean;
import com.wxj.bank.module.utils.UUIDUtils;
import com.wxj.bank.storage.dao.OrderUserMapper;
import com.wxj.bank.storage.model.OrderUser;
import com.wxj.bank.storage.model.OrderUserExample;
import com.wxj.bank.storage.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author shengtao
 * @Description:
 * @date 2019/02/19 16:46
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private OrderUserMapper mapper;

    @Override
    public OrderUser save(OrderUser user) {
        user.setId(UUIDUtils.genUUID());
        mapper.insert(user);
        return user;
    }

    @Override
    public void save(List<OrderUser> users) {
        users.stream().map(user -> {user.setId(UUIDUtils.genUUID());return user;}).collect(toList());
        this.mapper.batchInsert(users);
    }

    @Override
    public void remove(String id) {
        this.mapper.deleteByPrimaryKey(id);
    }

    @Override
    public OrderUser update(OrderUser user) {
        this.mapper.updateByPrimaryKey(user);
        return user;
    }

    @Override
    public OrderUser find(String id) {
        return this.mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderUser> find(OrderUser user) {
        OrderUserExample example = new OrderUserExample();
        OrderUserExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(user.getId())){
            criteria.andIdEqualTo(user.getId());
        }
        if(StringUtils.isNotBlank(user.getNikeName())){
            criteria.andNikeNameLike(user.getNikeName()+ Constants.SYMBOL_PERCENT);
        }
        return this.mapper.selectByExample(example);
    }

    @Override
    public PageBean<OrderUser> find(OrderUser user, PageBean<OrderUser> page) {
        OrderUserExample example = new OrderUserExample();
        OrderUserExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(user.getId())){
            criteria.andIdEqualTo(user.getId());
        }
        if(StringUtils.isNotBlank(user.getNikeName())){
            criteria.andNikeNameLike(user.getNikeName()+ Constants.SYMBOL_PERCENT);
        }
        Page<OrderUser> pages = PageHelper.startPage(page.getCurrentPage(),page.getPageSize());
        this.mapper.selectByExample(example);
        return new PageBean<>(pages);
    }
}
